package com.playfab;

<% if (isAndroid) { %>import android.util.Log;
<% } %>import com.playfab.internal.*;
import com.playfab.PlayFab<%- api.name %>Models.*;
import com.playfab.PlayFabErrors.*;
import com.playfab.PlayFabSettings;
import java.util.concurrent.*;
import java.util.*;
import com.google.gson.*;
import com.google.gson.reflect.*;

<%- generateApiSummary("    ", api, "description")
%>public class PlayFab<%- api.name %>API {
    private static Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create();
<% for(var i in api.calls) { var apiCall = api.calls[i]; %>
<%- generateApiSummary("    ", apiCall, "summary", ["@param request " + apiCall.request, "@return Async Task will return " + apiCall.result])
%>    @SuppressWarnings("unchecked")
    public static FutureTask<PlayFabResult<<%- apiCall.result %>>> <%- apiCall.name %>Async(final <%- apiCall.request %> request) {
        return new FutureTask(new Callable<PlayFabResult<<%- apiCall.result %>>>() {
            public PlayFabResult<<%- apiCall.result %>> call() throws Exception {
                return private<%- apiCall.name %>Async(request);
            }
        });
    }

<%- generateApiSummary("    ", apiCall, "summary", ["@param request " + apiCall.request, "@return " + apiCall.result])
%>    @SuppressWarnings("unchecked")
    public static PlayFabResult<<%- apiCall.result %>> <%- apiCall.name %>(final <%- apiCall.request %> request) {
        FutureTask<PlayFabResult<<%- apiCall.result %>>> task = new FutureTask(new Callable<PlayFabResult<<%- apiCall.result %>>>() {
            public PlayFabResult<<%- apiCall.result %>> call() throws Exception {
                return private<%- apiCall.name %>Async(request);
            }
        });
        try {
            task.run();
            return task.get();
        } catch(Exception e) {
            return null;
        }
    }

<%- generateApiSummary("    ", apiCall, "summary")
%>    @SuppressWarnings("unchecked")
    private static PlayFabResult<<%- apiCall.result %>> private<%- apiCall.name %>Async(final <%- apiCall.request %> request) throws Exception {
<%- getRequestActions("        ", apiCall) %>
        FutureTask<Object> task = PlayFabHTTP.doPost(<%- getUrlAccessor(apiCall.url) %>, request, <%- getAuthParams(apiCall) %>);
        task.run();
        Object httpResult = task.get();
        if (httpResult instanceof PlayFabError) {
            PlayFabError error = (PlayFabError)httpResult;
            if (PlayFabSettings.GlobalErrorHandler != null)
                PlayFabSettings.GlobalErrorHandler.callback(error);
            PlayFabResult result = new PlayFabResult<<%- apiCall.result %>>();
            result.Error = error;
            return result;
        }
        String resultRawJson = (String) httpResult;

        PlayFabJsonSuccess<<%- apiCall.result %>> resultData = gson.fromJson(resultRawJson, new TypeToken<PlayFabJsonSuccess<<%- apiCall.result %>>>(){}.getType());
        <%- apiCall.result %> result = resultData.data;
<%- getResultActions("        ", apiCall) %>
        PlayFabResult<<%- apiCall.result %>> pfResult = new PlayFabResult<<%- apiCall.result %>>();
        pfResult.Result = result;
        return pfResult;
    }
<% } %><% if (hasClientOptions) { %>
    public static void MultiStepClientLogin(Boolean needsAttribution) {
<% if (isAndroid) {
%>        if (needsAttribution && !PlayFabSettings.DisableAdvertising
        && (PlayFabSettings.AdvertisingIdType == null || PlayFabSettings.AdvertisingIdType == "")
        && (PlayFabSettings.AdvertisingIdValue == null || PlayFabSettings.AdvertisingIdValue == "")
        && PlayFabSettings.androidContext != null) {
            try {
                // Grab the android advertisingId and fill it in when needed
                PlayFabGetAdvertId.AdInfo adInfo = com.playfab.PlayFabGetAdvertId.getAdvertisingIdInfo(PlayFabSettings.androidContext);
                if (adInfo != null) {
                    PlayFabSettings.AdvertisingIdType = PlayFabSettings.AD_TYPE_ANDROID_ID;
                    PlayFabSettings.AdvertisingIdValue = adInfo.advertisingId;
                    PlayFabSettings.DisableAdvertising = adInfo.limitAdTrackingEnabled;
                } // else change nothing, I just can't provide any automatic values
            } catch (Exception e) {
                Log.e("MYAPP", "exception", e);
                PlayFabSettings.AdvertisingIdType = "error";
                PlayFabSettings.AdvertisingIdValue = null;
                PlayFabSettings.DisableAdvertising = null;
            }
        }

<% } %>        if (needsAttribution && !PlayFabSettings.DisableAdvertising && PlayFabSettings.AdvertisingIdType != null && PlayFabSettings.AdvertisingIdValue != null) {
            PlayFabClientModels.AttributeInstallRequest request = new PlayFabClientModels.AttributeInstallRequest();
            if (PlayFabSettings.AdvertisingIdType == PlayFabSettings.AD_TYPE_IDFA)
                request.Idfa = PlayFabSettings.AdvertisingIdValue;
            else if (PlayFabSettings.AdvertisingIdType == PlayFabSettings.AD_TYPE_ANDROID_ID)
                request.Adid = PlayFabSettings.AdvertisingIdValue;
            else
                return;
            FutureTask<PlayFabResult<AttributeInstallResult>> task = AttributeInstallAsync(request);
            task.run();
        }
    }
<% } %>}
